package demo1;

import java.util.Arrays;
import java.util.Comparator;


class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.name.compareTo(o.name);
    }
}

class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}

class StringComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}

public class TestDemo {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zs", 19);
        students[1] = new Student("lisi", 8);
        students[2] = new Student("abc", 78);
        AgeComparator ageComparator = new AgeComparator();
        Arrays.sort(students, ageComparator);
        System.out.println(Arrays.toString(students));
        System.out.println(students[0]);
    }

    public static void main2(String[] args) {
        Student student1 = new Student("zhangsan", 19);
        Student student2 = new Student("abc", 78);
        if(student2.compareTo(student1) > 0){
            System.out.println("fafafa");
        }
    }

    public static void main1(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zahngsan", 19);
        students[1] = new Student("lisi", 8);
        students[2] = new Student("abc", 78);
        System.out.println("排序前: " + Arrays.toString(students));
        Arrays.sort(students);
        System.out.println("排序后：" + Arrays.toString(students));
        System.out.println(students[0]);
    }
}
